﻿namespace OleDbDAL.Systems
{
    using DBUtility;
    using IDal.Systems;
    using Model.Systems;
    using System;
    using System.Data;
    using System.Text;

    public class UserInfoApp : IUserInfo
    {
        private string dbname = "Systems";

        public void Add(UserInfoEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into T_UserInfo(");
            builder.Append("UserId,UserTitle,MsnNum,QQNum,Birthday,email,HandSet,ext,Postitions,PhotoAddress,BeFondOf,Infos,NoteNum");
            builder.Append(")");
            builder.Append(" values (");
            builder.Append("" + model.UserId + ",");
            builder.Append("'" + model.UserTitle + "',");
            builder.Append("'" + model.MsnNum + "',");
            builder.Append("'" + model.QQNum + "',");
            builder.Append("'" + model.Birthday + "',");
            builder.Append("'" + model.email + "',");
            builder.Append("'" + model.HandSet + "',");
            builder.Append("'" + model.ext + "',");
            builder.Append("'" + model.Postitions + "',");
            builder.Append("'" + model.PhotoAddress + "',");
            builder.Append("'" + model.BeFondOf + "',");
            builder.Append("'" + model.Infos + "',");
            builder.Append("" + model.NoteNum + "");
            builder.Append(")");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public void Delete(int UserId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from T_UserInfo ");
            builder.Append(" where UserId=" + UserId);
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public DataTable GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_UserInfo ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by UserId ");
            return DbHelperOledb.Query(this.dbname, builder.ToString());
        }

        public int GetMaxId(string swhere)
        {
            return DbHelperOledb.GetMaxID(this.dbname, "UserId", "T_UserInfo", swhere);
        }

        public UserInfoEnt GetModel(int UserId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select UserId,UserTitle,MsnNum,QQNum,Birthday,email,HandSet,ext,Postitions,PhotoAddress,BeFondOf,Infos,NoteNum from T_UserInfo ");
            builder.Append(" where UserId=" + UserId);
            UserInfoEnt ent = new UserInfoEnt();
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            ent.UserId = UserId;
            if (table.Rows.Count > 0)
            {
                ent.UserTitle = table.Rows[0]["UserTitle"].ToString();
                ent.MsnNum = table.Rows[0]["MsnNum"].ToString();
                ent.QQNum = table.Rows[0]["QQNum"].ToString();
                if (table.Rows[0]["Birthday"].ToString() != "")
                {
                    ent.Birthday = DateTime.Parse(table.Rows[0]["Birthday"].ToString());
                }
                ent.email = table.Rows[0]["email"].ToString();
                ent.HandSet = table.Rows[0]["HandSet"].ToString();
                ent.ext = table.Rows[0]["ext"].ToString();
                ent.Postitions = table.Rows[0]["Postitions"].ToString();
                ent.PhotoAddress = table.Rows[0]["PhotoAddress"].ToString();
                ent.BeFondOf = table.Rows[0]["BeFondOf"].ToString();
                ent.Infos = table.Rows[0]["Infos"].ToString();
                if (table.Rows[0]["NoteNum"].ToString() != "")
                {
                    ent.NoteNum = int.Parse(table.Rows[0]["NoteNum"].ToString());
                }
                return ent;
            }
            return null;
        }

        public DataTable GetPageList(int PageSize, int PageIndex, string strWhere)
        {
            string orderString = "";
            int pageCount = 0;
            int recordCount = 0;
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_UserInfo ");
            return DbHelperOledb.ExecutePager(this.dbname, PageIndex, PageSize, "", builder.ToString(), strWhere, orderString, out pageCount, out recordCount, "UserId");
        }

        public void Update(UserInfoEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update T_UserInfo set ");
            builder.Append("UserTitle='" + model.UserTitle + "',");
            builder.Append("MsnNum='" + model.MsnNum + "',");
            builder.Append("QQNum='" + model.QQNum + "',");
            builder.Append("Birthday='" + model.Birthday + "',");
            builder.Append("email='" + model.email + "',");
            builder.Append("HandSet='" + model.HandSet + "',");
            builder.Append("ext='" + model.ext + "',");
            builder.Append("Postitions='" + model.Postitions + "',");
            builder.Append("PhotoAddress='" + model.PhotoAddress + "',");
            builder.Append("BeFondOf='" + model.BeFondOf + "',");
            builder.Append("Infos='" + model.Infos + "',");
            builder.Append("NoteNum=" + model.NoteNum + "");
            builder.Append(" where UserId=" + model.UserId + "");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }
    }
}

